package com.lhkj.ct.meta.modules.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.base.utils.file.word.PoiWordUtils;
import com.lhkj.ct.meta.modules.gauge.mapper.QnrMapper;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.questionnaire.mapper.QnrResultMapper;
import com.lhkj.ct.meta.modules.questionnaire.model.entity.TblQnrResult;
import com.lhkj.ct.meta.modules.questionnaire.model.vo.QnrResultVo;
import com.lhkj.ct.meta.modules.questionnaire.model.vo.QuestionnaireStatisticsVo;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrResultService;
import com.lhkj.ct.meta.modules.report.BaseFactory;
import com.lhkj.ct.meta.modules.report.word.QuestionnaireResultsHandle;
import com.lhkj.ct.meta.modules.report.word.QuestionnaireStatisticsReportHandle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 问卷作答记录 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-26
 */
@Service
public class QnrResultServiceImpl extends IServiceImpl<QnrResultMapper, TblQnrResult> implements QnrResultService {

    @Resource
    private QnrMapper qnrMapper;


    @Override
    public IPage<TblQnrResult> selectQnrResultPage(TblQnrResult result, PageQuery pageQuery) {
        Page<TblQnrResult> page = generatePage(pageQuery, SqlOrder.desc(TblQnrResult::getSubmissionTime));
        return getBaseMapper().selectQnrResultPage(page, result, pageQuery);
    }

    @Override
    public ViewAnswerVo viewAnswer(String rId) {
        final ViewAnswerVo answerVo = getBaseMapper().viewAnswer(rId);
        // 判断记录是否撤销
        Validate.isFalse(answerVo == null, "调查结果已删除或未完成,无法查看！");
        return answerVo;
    }

    @Override
    public void export(String rId) {
        QnrResultVo reportData = getBaseMapper().getReportData(rId);
        XWPFDocument document = BaseFactory.getReport(new QuestionnaireResultsHandle(), reportData);
        PoiWordUtils.wordDownload(document, reportData.getQnrName() + ".docx");
    }

    @Override
    public QuestionnaireStatisticsVo statistics(String qnrId, PageQuery pageQuery) {
        QuestionnaireStatisticsVo statistics = getBaseMapper().statistics(qnrId, pageQuery);
        double totalAvg = statistics.getQuestions().stream().mapToDouble(QuestionnaireStatisticsVo.Question::getAvg).reduce((a, b) -> a + b).getAsDouble();
        statistics.setTotalAvg(totalAvg);
        return statistics;
    }

    @Override
    public void exportStatistics(String qnrId, PageQuery pageQuery) {
        QuestionnaireStatisticsVo reportData = statistics(qnrId, pageQuery);
        XWPFDocument document = BaseFactory.getReport(new QuestionnaireStatisticsReportHandle(), reportData);
        PoiWordUtils.wordToPdfDownload(document, reportData.getQnrName() + ".pdf");
    }
}
